package edu.whu.demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import edu.whu.demo.domain.ActivityMember;
import edu.whu.demo.domain.ActivityMemberDTO;
import edu.whu.demo.exception.ActivityMemberException;
import edu.whu.demo.service.impl.ActivityMemberServiceImpl;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2022-11-24
 */
@RestController
@RequestMapping("/sign")
public class ActivityMemberController {
    @Autowired
    ActivityMemberServiceImpl activityMemberService;

    @Value("${PicPath.local}")
    String localPath;

    @Value("${PicPath.remote}")
    String remotePath;

    @Value("${server.port}")
    String port;

    @Value("${MyDomain}")
    String domain;

    @ApiOperation("根据活动id或者阶段和次数查询签到")
    @GetMapping("/{activityIdForSigns}")
    public IPage<ActivityMember> getSigns(@ApiParam("活动id")@PathVariable Long activityIdForSigns,
                                          @ApiParam("阶段")Integer stage,
                                          @ApiParam("该阶段第几次签到")Integer count,
                                          @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                          @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize){

        return activityMemberService.getSigns(activityIdForSigns, stage, count, pageNum, pageSize);
    }

    @ApiOperation("查询签到名单与状态")
    @GetMapping("")
    public IPage<ActivityMemberDTO> getMemberState(@ApiParam("阶段")Integer stage,
                                                   @ApiParam("该阶段第几次签到")Integer count,
                                                   @ApiParam("活动id")Long activityId,
                                                   @ApiParam("用户id")Long uid,
                                                   @ApiParam("用户名")String name,
                                                   @ApiParam("本次签到状态")String sign,
                                                   @ApiParam("页码")@RequestParam(defaultValue = "0")Integer pageNum,
                                                   @ApiParam("每页记录数") @RequestParam(defaultValue = "10")Integer pageSize)
    {
        Map<String,Object> condition=new HashMap<>();
        if(stage!=null) {
            condition.put("stage", stage);
        }
        if(count!=null) {
            condition.put("count", count);
        }
        if(activityId!=null) {
            condition.put("activity_id", activityId);
        }
        if(uid!=null) {
            condition.put("uid", uid);
        }
        if(name!=null) {
            condition.put("name","%"+name+"%");
        }
        if(sign!=null) {
            condition.put("sign", sign);
        }
        return activityMemberService.getMemberState(condition,pageNum,pageSize);
    }

    @ApiParam("发布签到")
    @PostMapping("")
    public ActivityMember addSign(@RequestBody ActivityMember activityMember) throws ActivityMemberException
    {
        return activityMemberService.addSign(activityMember);
    }

    @ApiOperation("修改发布签到")
    @PutMapping("")
    public Integer updateSign(@RequestBody List<ActivityMember> activityMembers) throws ActivityMemberException
    {
        if (activityMembers.size()>1)
        {
            //修改发布签到
            return activityMemberService.updateMultipleSign(activityMembers.get(0),activityMembers.get(1));
        }
        else
        {
            //修改签到状态
            activityMemberService.updateSingleSign(activityMembers.get(0));
            return 0;
        }
    }

    @ApiOperation("上传签到图片")
    @PostMapping("/setsign")
    public String updateSign(@RequestParam Long activityId, @RequestParam Long uid, @RequestPart("proof") MultipartFile proof) throws ActivityMemberException, IOException {
        //先确认是否存在开启的签到，然后才上传图片
        ActivityMember activityMember = activityMemberService.onlyLegalSign(activityId, uid);
        String fileName =  UUID.randomUUID()+".png";
        FileOutputStream fileOutputStream = new FileOutputStream(localPath+fileName);
        fileOutputStream.write(proof.getBytes());
        String proofPath = "http://"+domain+":"+port+"/"+remotePath+fileName;
        activityMember.setProof(proofPath);
        activityMember.setSign("待阅");
        activityMemberService.updateSingleSign(activityMember);
        return proofPath;
    }

    @ApiOperation("删除签到")
    @DeleteMapping("")
    public void deleteActivityMember(@RequestBody ActivityMember activityMember) throws ActivityMemberException {
        activityMemberService.deleteSign(activityMember);
    }


}

